//
//  ShopCartController.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/3/9.
//  消息控制器

#import "ShopCartController.h"

#import "BYShopCartEmptyView.h"
#import "ByShopCartCell.h"
#import "BYShopCartToolBar.h"

#import "BYShopCartModel.h"
#import "DBShopCart.h"
#import "DBShopCartDetail.h"
#import "BYConfirmOrderController.h"

@interface ShopCartController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) BYShopCartEmptyView   *emptyView;
@property (nonatomic, strong) BYTableView           *tableView;
@property (nonatomic, strong) BYShopCartToolBar     *toolBar;
@property (nonatomic, strong) NSMutableArray        *dataList;
@property (nonatomic, strong, readwrite) UIButton   *loginButton;


@end

@implementation ShopCartController

static NSString *kBYShopCartCellId = @"kBYShopCartCellId";

#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_VIEW_COLOR;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.emptyView];
    [self.view addSubview:self.toolBar];
    if ([AppConfig isLogin]) {
        self.toolBar.hidden = NO;
    }
    [self requestData];
    
    [kNotificationCenterSington addObserver:self selector:@selector(loginSuccessNotification) name:kFSNotificationNameLoginSuccess object:nil];
}

- (void)loginSuccessNotification {
    // 取出登录状态的字典
    NSMutableArray *loginArray = [kUserDefaultsSington valueForKey:kIsLoginArray];
    NSMutableDictionary *loginDict = [loginArray objectAtIndex:0];
    BOOL isLogin = [loginDict objectForKey:@"isLogin"];
    NSString *userType = [loginDict objectForKey:@"user_type"];
    NSLog(@"登录数据-Arry：%@", loginArray);
    NSLog(@"登录数据-Dict：%@", loginDict);
    NSLog(@"登录信息：%@ == %@", @(isLogin) , userType);
    
    // 用户是否登录
    if ([AppConfig isLogin]) {
        self.toolBar.hidden = NO;
    }else {
        self.toolBar.hidden = YES;
    }
    
//    if ([login isEqualToString:@"login"]) {
//        if ([userType isEqualToString:@"管理员"]) {
//            [self.view addSubview:self.mangeView];
//        }
//    } else {
//        [self.exitButton removeFromSuperview];
//    }
    
}

#pragma mark - RequestData
- (void)requestData {
    
    [[DBShopCartDetail sharedInstance] selectAllMethod];

    if ([AppConfig isLogin]) {
        NSArray *dataArray = [kUserDefaultsSington valueForKey:kShopCartDetial];
        self.dataList = [BYShopCartModel mj_objectArrayWithKeyValuesArray:dataArray] ;
        if (self.dataList.count >0) {
            [self.tableView reloadData];
            [self.emptyView removeFromSuperview];
        }else {
            [self.emptyView setData];
        }
    } else {
        [self.emptyView setData];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BYShopCartCell *cell = [tableView dequeueReusableCellWithIdentifier:kBYShopCartCellId];
    BYShopCartModel *model = self.dataList[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BYShopCartCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] init];
    header.backgroundColor = BACKGROUND_VIEW_COLOR;
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kAutoWithSize(10);
}



#pragma mark - LazyGet

- (BYTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BYTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = BACKGROUND_VIEW_COLOR;
        [_tableView registerClass:[BYShopCartCell class] forCellReuseIdentifier:kBYShopCartCellId];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0.0f;
        }
    }
    return _tableView;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

- (BYShopCartEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[BYShopCartEmptyView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight)];
    }
    return _emptyView;
}

- (BYShopCartToolBar *)toolBar {
    if (!_toolBar) {
        _toolBar = [[BYShopCartToolBar alloc] initWithFrame:CGRectMake(0, kScreenHeight-kSafeAreaTopHeight-kTabBarHeight-kAutoWithSize(105), kScreenWidth, kAutoWithSize(50))];
        _toolBar.hidden = YES;
        WS(weakSelf)
        _toolBar.checkOutBlock = ^{
            NSLog(@"结算事件");
            BYConfirmOrderController *controller = [[BYConfirmOrderController alloc] init];
            controller.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:controller animated:YES];
        };
    }
    return _toolBar;
}

@end


